package zl.ordermanager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zl.ordermanager.dao.PurchaseInvoiceDao;
import zl.ordermanager.dao.PurchaseOrderDao;
import zl.ordermanager.po.Tpurchaseinvoice;
import zl.ordermanager.po.Tpurchaseorder;
import zl.ordermanager.service.PurchaseInvoiceService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.PurchaseInvoice;
import zl.ordermanager.vo.Total;

@Service
public class PurchaseInvoiceServiceImpl implements PurchaseInvoiceService {

	@Autowired
	private PurchaseInvoiceDao purchaseInvoiceDao;

	@Autowired
	private PurchaseOrderDao purchaseOrderDao;

	@Override
	public DataGrid<PurchaseInvoice> dataGrid(PageHelper pageHelper, Total total) {
		DataGrid<PurchaseInvoice> dataGrid = new DataGrid<PurchaseInvoice>();
		List<PurchaseInvoice> purchaseInvoiceList = new ArrayList<PurchaseInvoice>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			params.put("keyword", "%%" + pageHelper.getKeywords() + "%%");
		}
		if (pageHelper.getAbstractMonth() != null && !pageHelper.getAbstractMonth().isEmpty()) {
			params.put("abstractMonth", pageHelper.getAbstractMonth());
		}
		if (pageHelper.getInvoiceDate() != null && !pageHelper.getInvoiceDate().isEmpty()) {
			params.put("invoiceDate", pageHelper.getInvoiceDate());
		}
		String hql = " from Tpurchaseinvoice t ";
		List<Tpurchaseinvoice> tpurchaseinvoiceList = purchaseInvoiceDao.find(hql + whereHql(pageHelper)
				+ orderHql(pageHelper), params, pageHelper.getPageNum(), pageHelper.getNumPerPage());
		if (tpurchaseinvoiceList != null && tpurchaseinvoiceList.size() > 0) {
			for (Tpurchaseinvoice tpurchaseinvoice : tpurchaseinvoiceList) {
				PurchaseInvoice purchaseInvoice = new PurchaseInvoice();
				BeanUtils.copyProperties(tpurchaseinvoice, purchaseInvoice);
				purchaseInvoiceList.add(purchaseInvoice);
			}
		}
		dataGrid.setList(purchaseInvoiceList);
		dataGrid.setTotal(purchaseInvoiceDao.count("select count(*) " + hql + whereHql(pageHelper), params));

		total.setAmount(purchaseInvoiceDao.sum("select sum(t.amount) " + hql + whereHql(pageHelper), params));
		return dataGrid;
	}

	private String whereHql(PageHelper pageHelper) {
		String whereString = "";
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			whereString += "where (t.orderNo like :keyword ";
			whereString += " or t.company like :keyword ";
			whereString += " or t.invoiceDate like :keyword ";
			whereString += " or t.takeDate like :keyword ";
			whereString += " or t.invoiceNo like :keyword ";
			whereString += " or t.orderType like :keyword ";
			whereString += " or t.ai like :keyword ";
			whereString += " or t.abstractMonth like :keyword ";
			whereString += " or t.invoiceType like :keyword ";
			whereString += " or t.orderAmount like :keyword ";
			whereString += " or t.amount like :keyword)";
		}
		if (pageHelper.getAbstractMonth() != null && !pageHelper.getAbstractMonth().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.abstractMonth = :abstractMonth)";
			} else {
				whereString += " and (t.abstractMonth = :abstractMonth)";
			}
		}
		if (pageHelper.getInvoiceDate() != null && !pageHelper.getInvoiceDate().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString += "where (t.invoiceDate = :invoiceDate)";
			} else {
				whereString += " and (t.invoiceDate = :invoiceDate)";
			}
		}

		return whereString;
	}

	private String orderHql(PageHelper pageHelper) {
		String orderString = "";
		if (pageHelper.getOrderField() != null && !pageHelper.getOrderField().isEmpty()
				&& pageHelper.getOrderDirection() != null && !pageHelper.getOrderDirection().isEmpty()) {
			orderString = " order by t." + pageHelper.getOrderField() + " " + pageHelper.getOrderDirection();
		}
		return orderString;
	}

	@Override
	public PurchaseInvoice get(Integer id) {
		Tpurchaseinvoice tpurchaseinvoice = purchaseInvoiceDao.get(Tpurchaseinvoice.class, id);
		if (tpurchaseinvoice == null) {
			return null;
		}
		PurchaseInvoice purchaseInvoice = new PurchaseInvoice();
		BeanUtils.copyProperties(tpurchaseinvoice, purchaseInvoice);
		return purchaseInvoice;
	}

	@Override
	public PurchaseInvoice getByInvoiceNo(String invoiceNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("invoiceNo", invoiceNo);
		Tpurchaseinvoice tpurchaseinvoice = purchaseInvoiceDao.get(
				"from Tpurchaseinvoice t where t.invoiceNo = :invoiceNo", params);
		if (tpurchaseinvoice == null) {
			return null;
		}
		PurchaseInvoice purchaseInvoice = new PurchaseInvoice();
		BeanUtils.copyProperties(tpurchaseinvoice, purchaseInvoice);
		return purchaseInvoice;
	}

	@Override
	public void add(PurchaseInvoice purchaseInvoice) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("invoiceNo", purchaseInvoice.getInvoiceNo());
		if (purchaseInvoiceDao.count("select count(*) from Tpurchaseinvoice t where t.invoiceNo = :invoiceNo", params) > 0) {
			throw new Exception("发票号已存在！");
		}

		String[] companys = purchaseInvoice.getCompany().split(",");
		Set<String> companySet = new TreeSet<String>();
		String finalCompany = "";
		for (String company : companys) {
			if (companySet.add(company)) {
				finalCompany += company + ",";
			}
		}
		finalCompany = finalCompany.substring(0, finalCompany.length() - 1);
		purchaseInvoice.setCompany(finalCompany);

		String[] ais = purchaseInvoice.getAi().split(",");
		Set<String> aiSet = new TreeSet<String>();
		String finalAi = "";
		for (String ai : ais) {
			if (aiSet.add(ai)) {
				finalAi += ai + ",";
			}
		}
		finalAi = finalAi.substring(0, finalAi.length() - 1);
		purchaseInvoice.setAi(finalAi);

		String[] orderAmounts = purchaseInvoice.getOrderAmount().split(",");
		double finalOrderAmount = 0;
		for (String orderAmount : orderAmounts) {
			if (!orderAmount.trim().isEmpty()) {
				finalOrderAmount += Double.parseDouble(orderAmount);
			}
		}
		purchaseInvoice.setOrderAmount(String.valueOf(finalOrderAmount));

		Tpurchaseinvoice tpurchaseinvoice = new Tpurchaseinvoice();
		BeanUtils.copyProperties(purchaseInvoice, tpurchaseinvoice);
		purchaseInvoiceDao.save(tpurchaseinvoice);
		params.clear();
		String[] orderNos = purchaseInvoice.getOrderNo().split(",");
		for (String orderNo : orderNos) {
			params.put("orderNo", orderNo);
			Tpurchaseorder tpurchaseorder = purchaseOrderDao.get("from Tpurchaseorder t where t.orderNo = :orderNo",
					params);
			if (tpurchaseorder != null) {
				tpurchaseorder.setInvoice("是");
			}
		}
	}

	@Override
	public void edit(PurchaseInvoice purchaseInvoice) throws Exception {

		String[] companys = purchaseInvoice.getCompany().split(",");
		Set<String> companySet = new TreeSet<String>();
		String finalCompany = "";
		for (String company : companys) {
			if (companySet.add(company)) {
				finalCompany += company + ",";
			}
		}
		finalCompany = finalCompany.substring(0, finalCompany.length() - 1);
		purchaseInvoice.setCompany(finalCompany);

		String[] ais = purchaseInvoice.getAi().split(",");
		Set<String> aiSet = new TreeSet<String>();
		String finalAi = "";
		for (String ai : ais) {
			if (aiSet.add(ai)) {
				finalAi += ai + ",";
			}
		}
		finalAi = finalAi.substring(0, finalAi.length() - 1);
		purchaseInvoice.setAi(finalAi);

		String[] orderAmounts = purchaseInvoice.getOrderAmount().split(",");
		double finalOrderAmount = 0;
		for (String orderAmount : orderAmounts) {
			if (!orderAmount.trim().isEmpty()) {
				finalOrderAmount += Double.parseDouble(orderAmount);
			}
		}
		purchaseInvoice.setOrderAmount(String.valueOf(finalOrderAmount));

		Tpurchaseinvoice tpurchaseinvoice = purchaseInvoiceDao.get(Tpurchaseinvoice.class, purchaseInvoice.getId());
		BeanUtils.copyProperties(purchaseInvoice, tpurchaseinvoice);
	}

	@Override
	public void delete(Integer id) {
		Tpurchaseinvoice tpurchaseinvoice = purchaseInvoiceDao.get(Tpurchaseinvoice.class, id);
		purchaseInvoiceDao.delete(tpurchaseinvoice);
		Map<String, Object> params = new HashMap<String, Object>();
		String[] orderNos = tpurchaseinvoice.getOrderNo().split(",");
		for (String orderNo : orderNos) {
			params.put("orderNo", orderNo);
			Tpurchaseorder tpurchaseorder = purchaseOrderDao.get("from Tpurchaseorder t where t.orderNo = :orderNo",
					params);
			if (tpurchaseorder != null) {
				tpurchaseorder.setInvoice("否");
			}
		}
	}

}
